package com.example.zce.back.controller.sys;

import com.alibaba.fastjson.JSON;
import com.example.zce.back.common.constant.REDIS_KEY_CONSTANT;
import com.example.zce.back.common.redis.RedisService;
import com.example.zce.library.common.bean.ResultMapBean;
import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.data.sys.constant.BackMenuEnum;
import com.example.zce.library.data.sys.entity.BackMenu;
import com.example.zce.library.data.sys.entity.BackRole;
import com.example.zce.library.data.sys.model.BackPermissionModel;
import com.example.zce.library.data.sys.service.BackMenuService;
import com.example.zce.library.data.sys.service.BackRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：菜单控制器
 *
 * @author zengyz
 * @date 2018/7/27 0:11
 */
@Controller
@RequestMapping("/sys/menu")
public class BackMenuController {

    @Autowired
    private BackMenuService backMenuService;

    @Autowired
    private RedisService redisService;

    /**
     * 菜单首页
     * @return
     */
    @GetMapping("/index.html")
    public ModelAndView indexHtml(){
        return new ModelAndView("sys/menu-index");
    }

    /**
     * 添加菜单页面
     * @return
     */
    @GetMapping("/add.html")
    public ModelAndView addHtml(){
        return new ModelAndView("sys/menu-add","type",BackMenuEnum.TYPE_MENU.getIntegerValue());
    }

    /**
     * 添加page页面
     * @param model
     * @param parentId
     * @return
     */
    @GetMapping("/page/add.html")
    public ModelAndView addPageHtml(Model model,String parentId){
        model.addAttribute("parentId",parentId);
        model.addAttribute("type",BackMenuEnum.TYPE_PAGE.getIntegerValue());
        return new ModelAndView("sys/menu-page-add");
    }

    /**
     * 修改页面
     * @param request
     * @return
     */
    @GetMapping("/update.html")
    public ModelAndView updateHtml(HttpServletRequest request){
        String id = request.getParameter("id");
        BackMenu backMenu = backMenuService.findById(id);
        request.setAttribute("backMenu",backMenu);
        return new ModelAndView("sys/menu-update");
    }

    /**
     * 修改page页面
     * @param request
     * @return
     */
    @GetMapping("/page/update.html")
    public ModelAndView updatePageHtml(HttpServletRequest request){
        String id = request.getParameter("id");
        BackMenu backMenu = backMenuService.findById(id);
        request.setAttribute("backMenu",backMenu);
        return new ModelAndView("sys/menu-page-update");
    }

    /**
     * page首页
     * @param request
     * @return
     */
    @GetMapping("/page/index.html")
    public ModelAndView permissionHtml(HttpServletRequest request){
        String id=request.getParameter("id");
        return new ModelAndView("sys/menu-page-index","parentId",id);
    }

    /**
     * page分页数据
     * @param request
     * @param parentId
     * @return
     */
    @ResponseBody
    @GetMapping("/page/list.json")
    public ResultTableBean<BackMenu> permissionJson(HttpServletRequest request,String parentId){
        Map params=new HashMap();
        params.put("pageNum",request.getParameter("page"));
        params.put("pageSize",request.getParameter("limit"));
        params.put("parentId",parentId);
        ResultTableBean<BackMenu> resultTableBean = backMenuService.findByParams(params);
        return resultTableBean;
    }

    /**
     * 菜单分页数据
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping("/list.json")
    public ResultTableBean<BackMenu> listJson(HttpServletRequest request){
        Map params=new HashMap();
        params.put("pageNum",request.getParameter("page"));
        params.put("pageSize",request.getParameter("limit"));
        params.put("type",BackMenuEnum.TYPE_MENU.getIntegerValue());
        ResultTableBean<BackMenu> resultTableBean = backMenuService.findByParams(params);
        return resultTableBean;
    }

    /**
     * 保存修改
     * @param backMenu
     * @return
     */
    @ResponseBody
    @PostMapping("/saveupdate.json")
    public ResultMapBean saveupdateJson(BackMenu backMenu){
        try {
            int i = backMenuService.saveOrUpdate(backMenu);
            if (i!=0){
                redisService.set(REDIS_KEY_CONSTANT.MENU_TREE,JSON.toJSONString(backMenuService.getBackMenuTree()));
                return ResultMapBean.ok();
            }else{
                return ResultMapBean.error(501,"操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/delete.json")
    public ResultMapBean deleteJson(String id){
        try {
            int i = backMenuService.delete(id);
            if (i!=0){
                redisService.set(REDIS_KEY_CONSTANT.MENU_TREE,JSON.toJSONString(backMenuService.getBackMenuTree()));
                return ResultMapBean.ok();
            }else{
                return ResultMapBean.error(501,"操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMapBean.error(500,"系统错误");
        }
    }
}
